
layui.use(['form', 'layer', 'table', 'laytpl', 'laydate'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
        laydate = layui.laydate;

    var userInfo = JSON.parse(window.sessionStorage.getItem('userInfo'));
    var ViewModel = {
        LoadDate: function () {
            laydate.render({
                elem: '#startdate'
            });
            laydate.render({
                elem: '#enddate'
            });
        },
        Initialize: function () {
            ViewModel.LoadDate();
        }
    };

    ViewModel.Initialize();

    var cols = [
        { type: "checkbox", fixed: "left", width: 50 },
        { field: "Id", title: '编号', width: 70, align: "center" },
        { field: 'ManagerName', title: '登录名', minWidth: 50, align: "center" },
        { field: 'ManagerRealName', title: '真实姓名', minWidth: 50, align: "center" },
        { field: 'BankCard', title: '银行卡号', minWidth: 80, align: "center" },
        { field: 'BankType', title: '发卡城市', minWidth: 30, align: "center", templet: function (d) { if (d.BankCity && d.BankProvince) { return d.BankCity + ':' + d.BankProvince; } else { return "-"; } } },
        { field: 'ApplyTime', title: '申请时间', minWidth: 50, align: "center" },
        { field: 'RoleId', title: '所属级别', minWidth: 30, align: "center", templet: function (d) { return ({ "1": "超级管理员", "2": "管理员", "3": "营业部", "4": "高级代理", "5": "中级代理", "6": "直客" })[d.RoleId]; } },
        { field: 'ExtractRMBMoney', title: '提现金额', minWidth: 30, align: "center" },
        { field: 'AuditTime', title: '审核时间', minWidth: 100, align: "center" },
        { field: 'State', title: '申请状态', minWidth: 30, fixed: "right", align: "center", templet: "#State" }
    ];

    if (userInfo && userInfo.roleId <= 3) {
        $(".apply").hide();
    } else {
        cols.push({ title: '操作', minWidth: 80, templet: '#managerListBar', fixed: "right", align: "center" });
    }
    //角色列表
    var tableIns = table.render({
        elem: '#managerList',
        url: '/RebateExtract/LoadData/',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 10,
        id: "managerListTable",
        cols: [cols]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        //if ($(".searchVal").val() !== '') {
            table.reload("managerListTable", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val(),  //搜索的关键字
                    start: $(".startdate").val(),
                    end: $(".enddate").val()
                }
            });
        //} else {
        //    layer.msg("请输入搜索的内容");
        //}
    });

    //添加用户
    function addManager(edit) {
        var tit = "申请提现";
        if (edit) {
            tit = "取消提现";
        }
        var index = layui.layer.open({
            title: tit,
            type: 2,
            anim: 1,
            area: ['550px', '50%'],
            content: "/RebateExtract/AddOrModify/",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                if (edit) {
                    body.find("#Id").val(edit.Id);
                    //body.find(".ProjectInfo").val(edit.ProjectCode);
                    //body.find(".ProjectPrice").val(edit.ProjectPrice);
                    
                    form.render();
                }
            }
        });
    }
 
    $(".apply").click(function () {
        addManager();
    });

    //批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('managerListTable'),
            data = checkStatus.data,
            managerId = [];
        if (data.length > 0) {
            for (var i in data) {
                managerId.push(data[i].Id);
            }
            layer.confirm('确定删除选中的用户？', { icon: 3, title: '提示信息' }, function (index) {
                //获取防伪标记
                del(managerId);
            });
        } else {
            layer.msg("请选择需要删除的用户");
        }
    });

    //列表操作
    table.on('tool(managerList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;

        if (layEvent === 'edit') { //编辑
            addManager(data);
        } else if (layEvent === 'del') { //删除
            layer.confirm('确定取消审核？', { icon: 3, title: '提示信息' }, function (index) {
                //del(data.Id);
                audit(data.Id, 2);
            });
        } else if (layEvent === 'state') {
            if (userInfo && userInfo.roleId == 3) {
                // 只有营业厅可以操作
                layer.confirm('您确认要审核通过吗？', {
                    icon: 3,
                    title: '审核提示',
                    btn: ['通过', '不通过', '取消'],
                    btn3: function (index, layero) {
                        layer.close(index);
                    },
                    cancel: function (index) {
                        layer.close(index);
                    }
                }, function (index) {
                    audit(data.Id, 1);
                    layer.close(index);
                }, function (index) {
                    audit(data.Id, 3);
                    layer.close(index);
                });
            }

        }
    });

    function del(managerId) {
        $.ajax({
            type: 'POST',
            url: '/RebateExtract/Delete/',
            data: { managerId: managerId },
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (data) {//res为相应体,function为回调函数
                var index = layer.msg(data.ResultMsg, {
                    time: 2000 //20s后自动关闭
                }, function () {
                    tableIns.reload();
                    layer.close(index);
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }

    function audit(id, state) {
        $.ajax({
            type: 'POST',
            url: '/RebateExtract/AddOrModify/',
            data: {
                Id: id,
                State: state
            },
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (data) {//res为相应体,function为回调函数
                var alertIndex = layer.alert(data.ResultMsg, { icon: 3 }, function () {
                    layui.table.reload('managerListTable', {
                        where: {}
                        , page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    top.layer.close(alertIndex);
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }

});
